package com.castle.fortress.admin.system.bindapi.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.codec.Base64;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.core.util.XmlUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.castle.fortress.admin.system.bindapi.service.BindApiService;
import com.castle.fortress.admin.system.dto.ConfigApiDto;
import com.castle.fortress.admin.utils.SignUtils;
import com.castle.fortress.common.exception.BizException;
import com.castle.fortress.common.utils.CommonUtil;
import com.qcloud.cos.auth.BasicCOSCredentials;
import com.qcloud.cos.auth.COSSigner;
import com.qcloud.cos.http.HttpMethodName;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.nlp.v20190408.NlpClient;
import com.tencentcloudapi.nlp.v20190408.models.*;
import org.lionsoul.jcseg.ISegment;
import org.lionsoul.jcseg.dic.ADictionary;
import org.lionsoul.jcseg.dic.DictionaryFactory;
import org.lionsoul.jcseg.extractor.impl.TextRankKeywordsExtractor;
import org.lionsoul.jcseg.segmenter.SegmenterConfig;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.io.StringReader;
import java.net.URLEncoder;
import java.util.*;

/**
 * Jcseg关键字提取
 *
 * @@author castle
 */

public class JcsegBindApiClient implements BindApiService {
    private Credential cred;
    private String region;
    private ConfigApiDto platform;


    private String lexiconPath;

    public JcsegBindApiClient() {

    }

    public JcsegBindApiClient(ConfigApiDto apiPlatform) {
        if (apiPlatform == null || apiPlatform.getParamMap() == null || apiPlatform.getParamMap().get("folderPath") == null || StringUtils.isBlank(apiPlatform.getParamMap().get("folderPath").toString())) {
            throw new BizException("字典配置注入异常");
        }
        this.lexiconPath = apiPlatform.getParamMap().get("folderPath").toString();
    }

    public SegmenterConfig initConfig() {
        //1, 创建Jcseg ISegment分词对象
        SegmenterConfig config = new SegmenterConfig(true);
        config.setAppendCJKSyn(false);      //设置关闭同义词追加
        config.setKeepUnregWords(false);    //设置去除不识别的词条
        config.setClearStopwords(true);
        String[] lexiconPath = {this.lexiconPath};
        config.setLexiconPath(lexiconPath);
        return config;
    }

    public List<String> keywordsExtractor(String contents, int keywordsNum) {
        SegmenterConfig config = initConfig();
        if (StrUtil.isEmpty(contents)) {
            return new ArrayList<>();
        }
        //默认提取10个关键字
        if (keywordsNum < 1) {
            keywordsNum = 10;
        }
        contents = contents.replaceAll("\\d", "");
        ADictionary dic = DictionaryFactory.createSingletonDictionary(config, true);
        ISegment seg = ISegment.COMPLEX.factory.create(config, dic);
        //2, 构建TextRankKeywordsExtractor关键字提取器
        TextRankKeywordsExtractor extractor = new TextRankKeywordsExtractor(seg);
        extractor.setMaxIterateNum(100);        //设置pagerank算法最大迭代次数，非必须，使用默认即可
        extractor.setWindowSize(5);             //设置textRank计算窗口大小，非必须，使用默认即可
        extractor.setKeywordsNum(keywordsNum);           //设置最大返回的关键词个数，默认为10
        //3, 从一个输入reader输入流中获取关键字
        List<String> keywords = new ArrayList<>();
        try {
            keywords = extractor.getKeywords(new StringReader(contents));
            System.out.println("keywords:" + keywords);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        System.out.println("关键字提取结果：" + keywords);
        return keywords;
    }

    @Override
    public String[] keywordsExtraction(String platformCode, String text, Long num) {
        return this.keywordsExtractor(text, num.intValue()).toArray(new String[0]);
    }

    @Override
    public String autoSummarization(String platformCode, String text, Long Length) {
        throw new BizException("JCSEG DOES NOT SUPPORT AUTO SUMMARIZATION");
    }

    @Override
    public Map<String, Object> textCorrection(String platformCode, String text) {
        throw new BizException("JCSEG DOES NOT SUPPORT TEXT CORRECTION");
    }

    @Override
    public Map<String, Object> textAudit(String platformCode, String text, String detectType) {
        throw new BizException("JCSEG DOES NOT SUPPORT TEXT AUDIT");

    }
}